/*
 * 验证手机号是否合格
 * true--说明合格
 */
export function isCellPhone(rule, value, callback) {
  const reg = /^1(3|4|5|6|7|8|9)\d{9}$/;
  if (value == '' || value == undefined || value == null) {
    callback();
  } else {
    if ((!reg.test(value)) && value != '') {
      callback(new Error('请输入正确的手机号！'));
    } else {
      callback();
    }
  }
}
/*
 * 验证身份证号是否合格
 * true--说明合格
 */
export function isIdCard(rule,idCardStr, callback) {
  let idcardReg = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
  if (idcardReg.test(idCardStr)) {
    callback();
  } else {
    callback(new Error('请输入正确的身份证号！'));
  }
}

// 是否为6位数的密码（数字和字母组成的）
export function validatePassWord(rule, value, callback) {
  const reg = /^[0-9A-Za-z]{6,}$/;
  if (value == '' || value == undefined || value == null) {
    callback();
  } else {
    if ((!reg.test(value)) && value != '') {
      callback(new Error('请输入6位数字或字母的密码'));
    } else {
      callback();
    }
  }
}

//邮箱验证
export function validateEmail(rule, value, callback) {
  const reg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
  if (value == '' || value == undefined || value == null) {
    callback();
  } else {
    if ((!reg.test(value)) && value != '') {
      callback(new Error('请输入正确的邮箱'));
    } else {
      callback();
    }
  }
}

//银行卡号验证
export function validateBankCard(rule, value, callback) {
  const reg = /^([1-9]{1})(\d{14}|\d{18})$/;
  if (value == '' || value == undefined || value == null) {
    callback();
  } else {
    if ((!reg.test(value)) && value != '') {
      callback(new Error('请输入正确的银行卡号'));
    } else {
      callback();
    }
  }
}


// 手机号中间四位加*
export function mobileStr(str) {
    if(str.length>7){
      return  str.substring(0,3)+'****'+str.substring(7,str.length);
    }else {
      return str.substring(0,str.length-1)+'****';
    }
}

export function timestamp(url,data){  //获取当前时间戳
  let newTime = new Date();
  return newTime.getTime();
};


export default {
  isCellPhone,
  isIdCard,
  validateEmail,
  validateBankCard,
  validatePassWord,
  mobileStr,
  timestamp
}